<template>
  <div class="home">
    <div class="headers">
      <Header></Header>
			  <ly-tabs 
	              v-model="value" 
				  activeColor="#b0352f"
			      @change="changeTab">
			  	<ly-tab-item 
				  v-for="(item,index) in items" 
			  	 :name="item.id" :title="item.label" 
			  	 :key="index" />
			  </ly-tabs>
    </div>

    <section class="wrapper" ref="wrapper">
      <div class="wrapper-main">
        <div v-for="(item, index) in newData" :key="index">
          <Swiper
            v-if="item.type === 'swiperList'"
            :swiperList="item.data"
          ></Swiper>
          <Icons
            v-if="item.type === 'iconsList'"
            :iconsList="item.data"
          ></Icons>
          <Recommend
            v-if="item.type === 'recommendList'"
            :recommendList="item.data"
          ></Recommend>
          <Ad v-if="item.type === 'adList'" :adList="item.data"></Ad>
          <Like v-if="item.type === 'likeList'" :likeList="item.data"></Like>
        </div>
      </div>
    </section>
    <tabbar></tabbar>
  </div>
</template>

<script>
// @ is an alias to /src
import Header from "@/components/home/Header.vue";
import Swiper from "@/components/home/Swiper.vue";
import Icons from "@/components/home/Icons.vue";
import BetterScroll from "better-scroll";
import Like from "@/components/home/Like.vue";
import Recommend from "@/components/home/Recommend.vue";
import Ad from "@/components/home/Ad.vue";

import Tabbar from "@/components/common/Tabbar.vue";


import http from '@/common/api/request';

export default {
  components: {
    Header,
    Swiper,
    Icons,
    Recommend,
    Like,
    Ad,
    Tabbar,
  },
  data() {
    return {
      value: "1",
      items: [],
      newData: [],
      options: {
        activeColor: "#b0352f",
      },
    };
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {

      let res = await http.$axios({url: "/api/index_list/0/data/1" })

      this.items = Object.freeze(res.topBar);
      this.newData = Object.freeze(res.data);

      //当dom都加载完毕后再去执行，相当于window.onload
      this.$nextTick(()=>{
        new BetterScroll(this.$refs.wrapper, {
          movable: true,
          zoom: true,
          click:true
        });
      })
    },
    async addData(name){
      let res = await http.$axios({ url: "/api/index_list/"+name+"/data/1" });
      if(res.constructor != Array){
        this.newData = res.data;
      }else{
        this.newData = res;
      }

            //当dom都加载完毕后再去执行，相当于window.onload
      this.$nextTick(()=>{
        new BetterScroll(this.$refs.wrapper, {
          movable: true,
          zoom: true,
          click:true
        });
      })
    },
    changeTab(name) {
      this.addData(name)
    },
  },
  mounted() {

  },
};
</script>

<style scoped>
.home {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
.headers {
  width: 100%;
  height: 2.5714rem;
}

section {
  flex: 1;
  overflow: hidden;
  /* height: 11.4286rem; */
}
</style>
